﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;

namespace Chefbook.HelperMethods
{
    /// <summary>
    /// Extension methods for handling potentially null values
    /// </summary>
    public static class NullHandlingExtensionMethods
    {
        /// <summary>
        /// Performs an action if this is not null
        /// </summary>
        /// <param name="valueToCheck">The object to check for null</param>
        /// <param name="actionToPerform">The action to perform if this is not null</param>
        public static void PerformIfNotNull(this object valueToCheck, Action actionToPerform)
        {
            if (valueToCheck != null)
            {
                actionToPerform();
            }
        }

        /// <summary>
        /// Compares if value1.Equals(value2) even if value1 can be null
        /// </summary>
        /// <param name="value1">The first value</param>
        /// <param name="value2">The second value</param>
        /// <returns>True if value1.Equals(value2) or if they are both null, else false</returns>
        public static bool NullSafeEquals(this object value1, object value2)
        {

            if (value1 == null)
            {
                if (value2 != null) return false;
                else return true;
            }
            return value1.Equals(value2);
            
        }
    }
}
